package com.cirkana.viewer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.cirkana.ViewerActivity;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.viewer.data.route.RouteDetail;
import com.cirkana.viewer.data.security.User;
import com.cirkana.viewer.map.MapActivity;
import com.cirkana.viewer.map.MapActivity.MapPassData;
import com.cirkana.viewer.route.SelectRouteActivity;
import com.cirkana.viewer.route.SelectRouteActivity.SelectRoutePassData;
import com.cirkana.viewer.security.SignInActivity;
import com.cirkana.viewer.security.SignInActivity.SignInPassData;

public class MainActivity extends ViewerActivity {

	private static final int REQUEST_SIGNIN = 1;
	private static final int REQUEST_SELECTROUTE = 2;
	private static final int REQUEST_MAP = 3;

	private User currentUser;
	private RouteDetail currentRoute;

	private Button selectedRouteButton;
	private Button startButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			startActivityForResult(SignInActivity.class, REQUEST_SIGNIN, new SignInActivity.SignInPassData(this.currentUser));
		} catch (BinarySerializationException e) {
			openMessageBox("Error", e.getMessage());
		}

		setContentView(R.layout.main);
		this.selectedRouteButton = (Button) findViewById(R.id.main_selected_route);
		this.startButton = (Button) findViewById(R.id.main_start);

		this.selectedRouteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					startActivityForResult(SelectRouteActivity.class, REQUEST_SELECTROUTE, new SelectRoutePassData(currentRoute));
				} catch (BinarySerializationException e) {
					openMessageBox("Error", e.getMessage());
				}
			}

		});

		this.startButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					startActivityForResult(MapActivity.class, REQUEST_MAP, new MapPassData(currentRoute, currentUser));
				} catch (BinarySerializationException e) {
					showErrorMessage(e);
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {

		case REQUEST_SIGNIN:
			switch (resultCode) {
			case RESULT_OK:
				try {
					SignInPassData d = (SignInPassData) getResponse(data, SignInPassData.class);
					this.currentUser = d.getUser();
				} catch (BinarySerializationException e) {
					showErrorMessage(e);
				}
				break;
			}
			break;

		case REQUEST_SELECTROUTE:
			switch (resultCode) {
			case RESULT_OK:
				try {
					SelectRoutePassData d = (SelectRouteActivity.SelectRoutePassData) getResponse(data, SelectRouteActivity.SelectRoutePassData.class);
					this.currentRoute = d.getSelectedRouteDetail();
					this.selectedRouteButton.setText(this.currentRoute.getTitle());
					this.startButton.setEnabled(true);
				} catch (BinarySerializationException e) {
					showErrorMessage(e);
				}
				break;
			}
			break;

		case REQUEST_MAP:

		}

	}

}
